package com.moon.controller;

import com.moon.dao.PoetryMapper;
import com.moon.entity.Poetry;
import com.moon.esmodel.PoetryEs;
import com.moon.esrepo.PoetryEsRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/p")
public class PoetryEsController {
    @Autowired
    private PoetryMapper poetryMapper;
    @Autowired
    private PoetryEsRepository poetryEsRepository;

    @RequestMapping("/importdata")
    public String importdata(){
        List<Poetry> list = poetryMapper.selectAll();
        for (Poetry p : list) {
            PoetryEs esp = new PoetryEs();
            BeanUtils.copyProperties(p,esp);        //对象克隆
            poetryEsRepository.save(esp);
            System.out.println("导入成功:"+esp.getTitle());
        }
        return "wn32/success";
    }
    @RequestMapping("/search")
    @ResponseBody
    public List<PoetryEs> search(String word){
        List<PoetryEs> byContentIn = poetryEsRepository.findByContentIn(word);
        return byContentIn;
    }
}
